//
//  UIView+BlankPage.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "UIView+BlankPage.h"
#import "UIColor+HexColor.h"
#import <Masonry/Masonry.h>
#import <objc/runtime.h>
#import "UIImage+image.h"
#import "QNCateTool.h"


NSInteger blankTag = 11111;
NSInteger blankImgTag = 111111;
NSInteger blankLabTag = 222222;
NSInteger blankBtnTag = 333333;



@implementation UIView (BlankPage)

- (UIView *)blankPage{
    UIView* blank = [self viewWithTag:blankTag];
    if (!blank) {
        blank = [[UIView alloc] init];
        blank.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        blank.tag = blankTag;
        [self addSubview:blank];
    }
    return blank;
}

- (UIImageView *)img{
    UIImageView* img = [self.blankPage viewWithTag:blankImgTag];
    if (!img) {
        img = [UIImageView new];
        img.tag = blankImgTag;
        [self.blankPage addSubview:img];
    }
    return img;
}

- (UILabel *)lab{
    UILabel* lab = [self.blankPage viewWithTag:blankLabTag];
    if (!lab) {
        lab = [UILabel new];
        lab.numberOfLines = 0;
        lab.tag = blankLabTag;
        lab.font = [UIFont systemFontOfSize:16];
		lab.textColor = [UIView color_DarkGray];
//		[GPTheme primaryTextColor];
        [self.blankPage addSubview:lab];
    }
    return lab;
}

+ (UIColor *)color_DarkGray
{
	return [UIColor colorWithHexString:@"#666666"];
}


- (UIButton *)refreshBtn{
    UIButton* btn = [self.blankPage viewWithTag:blankBtnTag];
    if (!btn) {
        btn = [UIButton new];
        btn.tag = blankBtnTag;
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitle:@"刷新" forState:UIControlStateNormal];
        btn.contentEdgeInsets = UIEdgeInsetsMake(7, 24, 7, 24);
        [btn setTitleColor:[UIColor colorWithHexString:@"#626262"] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 1;
        btn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
        btn.layer.borderWidth = 1;
        [btn sizeToFit];
        btn.layer.cornerRadius = btn.bounds.size.height*0.5;
        [self.blankPage addSubview:btn];
    }
    return btn;
}

- (void)layoutBlank:(CGFloat)offset {
    [self.blankPage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(offset, 0, 0, 0));
        make.size.equalTo(self);
    }];
    
    [self.img mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.blankPage);
        make.centerY.equalTo(self.blankPage).multipliedBy(459/603.0);
        make.height.width.equalTo(self.blankPage.mas_width).multipliedBy(143/375.0);
    }];
    
    [self.lab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.blankPage);
        make.top.equalTo(self.img.mas_bottom).offset(15);
//        make.height.equalTo(@14);
    }];
    UIButton* btn = [self.blankPage viewWithTag:blankBtnTag];
    if (btn) [btn removeFromSuperview];
    
    [self bringSubviewToFront:self.blankPage];
}

- (void)layoutNoNetwork{
    [self.blankPage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.size.equalTo(self);
    }];
    
    [self.img mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.blankPage);
        make.bottom.equalTo(self.blankPage).multipliedBy(268.5/603.0);
        make.height.width.equalTo(self.blankPage.mas_width).multipliedBy(143/375.0);
    }];
    
    [self.lab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.blankPage);
        make.top.equalTo(self.img.mas_bottom).offset(15);
    }];
    
    [self.refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.blankPage);
        make.top.equalTo(self.lab.mas_bottom).offset(30);
    }];
    
    [self bringSubviewToFront:self.blankPage];
}

- (void)setBlankImageName:(UIImage *)img titleName:(NSString *)title {
    [self setBlankImageName:img titleName:title offset:0];
}

- (void)setBlankImageName:(UIImage *)img titleName:(NSString *)title offset:(CGFloat)offset{
    UILabel* lab = self.lab;
    lab.text = title;
    lab.font = [UIFont systemFontOfSize:14];
    lab.textColor = [UIColor colorWithHexString:@"#666666"];
    lab.numberOfLines = 0;
    self.img.image = img;
//    [UIImage imageNamed:imgName];
    self.blankPage.hidden = NO;
    [self layoutBlank:offset];
}
    
//- (void)setBlankImageName:(NSString *)imgName titleName:(NSString *)title offset:(CGFloat)offset {
//    UILabel* lab = self.lab;
//    lab.text = title;
//    lab.font = [UIFont systemFontOfSize:14];
//    lab.textColor = [GPTheme primaryTextColor];
//    lab.numberOfLines = 0;
//    self.img.image = [UIImage imageNamed:imgName];
//    self.blankPage.hidden = NO;
//    [self layoutBlank:offset];
//}

/**
 设置空白页
 
 @param img 图片名
 @param title 标题
 @param alignment 位置
 */
- (void)setBlankImageName:(UIImage *)img
                titleName:(NSString *)title
                alignment:(NSTextAlignment)alignment {
    [self setBlankImageName:img titleName:title];
    self.lab.textAlignment = alignment;
}

- (void (^)(void))refreshBlock{
	
	return objc_getAssociatedObject(self, @selector(refreshBlock));
	
}

- (void)setRefreshBlock:(void (^)(void))refreshBlock{
	if (refreshBlock) {
		objc_setAssociatedObject(self, @selector(refreshBlock), refreshBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
	}
}

- (void)noNetworkWithRefreshBlock:(void(^)(void))refresh{
    self.blankPage.hidden = NO;
//     Category的东西会回到类中，通过Category找不到执行文件所在的bundle
#pragma mark -通过QNCateTool来找代码位置
    self.img.image = [UIImage gp_imgWithCls:[QNCateTool class] podName:@"QNReuseTool" imgName:@"gp_blank_no_network"];
//    [UIImage imageNamed:@"blank_no_network"];
    NSMutableParagraphStyle* style = [NSMutableParagraphStyle new];
    style.lineSpacing = 10;
    style.alignment = NSTextAlignmentCenter;
    NSMutableAttributedString* arrStr = [[NSMutableAttributedString alloc] initWithString:@"网络竟然崩溃了" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#9B9B9B"]}];
    [arrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n别紧张，试试看刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#B2B2B2"]}]];
    self.lab.attributedText = arrStr;
	
	
    if (![self.blankPage viewWithTag:blankBtnTag]) {
//		void(^)(void)
		self.refreshBlock = refresh;
		[self.refreshBtn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
	
//        [[self.refreshBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//            if (refresh) {
//                refresh();
//            }
//        }];
    }
    [self layoutNoNetwork];
}

- (void)btnClicked{
	if (self.refreshBlock) {
		self.refreshBlock();
	}
}

- (void)showDefaultBlankContent{
    [self setNeedsLayout];
    [self layoutIfNeeded];
    [[self.blankPage viewWithTag:blankImgTag] removeFromSuperview];
    [[self.blankPage viewWithTag:blankBtnTag] removeFromSuperview];
    self.blankPage.hidden = NO;
    self.lab.text = @"暂无内容";
    self.lab.font = [UIFont systemFontOfSize:20];
    self.lab.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.lab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
}
@end
